//
//  RNOLClaimTransferViewController.m
//  RongNiuOnline
//
//  Created by apple on 2018/4/19.
//  Copyright © 2018年 rongniu. All rights reserved.
//

#import "RNOLClaimTransferViewController.h"
#import "RNOLNewHandHeaderView.h"
#import "RNOLBidRecordListViewController.h"
#import "RNOLBuyClaimViewController.h"
#import "RNOLInvestDetailListCell.h"
#import "RNOLClaimTraformHeader.h"

@interface RNOLClaimTransferViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSDictionary *dataDict;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,strong) RNOLClaimTraformHeader *header;
@property (nonatomic,strong) CountDown *countDownObj;
@property (nonatomic,copy) NSString *timeStr;

@end

@implementation RNOLClaimTransferViewController

- (NSMutableArray *)dataSource
{
    if (_dataSource==nil) {
        _dataSource =[[NSMutableArray alloc]init];
    }
    return _dataSource;
}

- (RNOLClaimTraformHeader *)header{
    if (!_header) {
        _header = [RNOLClaimTraformHeader FromXIB];
        _header.backgroundColor = kAppMainColor;
        _header.frame = CGRectMake(0, 0, KWIDTH , 120);
    }
    return _header;
}
- (CountDown *)countDownObj{
    if (!_countDownObj) {
        _countDownObj = [[CountDown alloc] init];
    }
    return _countDownObj;
}


- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH , KHIGHT -kNavBarHeight- 50) style:UITableViewStyleGrouped];
        _tableView.bounces = NO;
        _tableView.backgroundColor = kBgColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        adjustsScrollViewInsets_NO(_tableView, self);
        _tableView.height -= kBottomHeight;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        [_tableView regsiterCellWithCellClass:[RNOLInvestDetailListCell class] isNib:YES];
        [self.view addSubview: _tableView];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavBarTitleViewWithText:@"债权转让"];
    self.view.backgroundColor = kBgColor;
    [self setup];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (void)setup{
    self.dataDict = @{
                      @"0" : @[@"项目名称",@"项目类型",@"债权价值",@"回款方式",@"起息时间",@"下次回款",@"剩余时间",@"协       议"],
                      @"1" : @[@"项目信息",@"投标记录"]
                      };
    self.dataSource = [@[@"[转]投资计划134512223",@"房抵贷",@"52000.00元",@"先息后本",@"承接后当日起息",@"2018-02-04",@"00天9小时45分53秒",@"债权转让协议"] mutableCopy];
    self.tableView.tableHeaderView = self.header;
    [self.tableView reloadData];
    
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, KHIGHT - kNavBarHeight - 50, KWIDTH , 50)];
    bottomView.y -= kBottomHeight;
    bottomView.backgroundColor = KWhiteColor;
    [self.view addSubview: bottomView];
    
    UIButton *calButton = [UIButton buttonWithType:UIButtonTypeCustom];
    calButton.backgroundColor = KWhiteColor;
    [calButton setImage:[UIImage imageNamed:@"rnol_calcutor"] forState:UIControlStateNormal];
    calButton.frame = CGRectMake(0, 0, 50 , 50);
    [calButton addTarget:self action:@selector(alertCaltorAction:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview: calButton];
    
    UIButton *immediatelyLoanBtn = [RNOLUnitTool createRNOLStyleBlueButtonWithButtonTitle:@"立即投标"];
    immediatelyLoanBtn.x = 50;
    immediatelyLoanBtn.width = KWIDTH - 50;
    immediatelyLoanBtn.height = 50;
    immediatelyLoanBtn.y = 0;
    [immediatelyLoanBtn addTarget:self action:@selector(immediatelyLoanAction) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview: immediatelyLoanBtn];

    @weakify(self);
    [self.countDownObj countDownWithStratDate:[NSDate date] finishDate: [[NSDate date] dateByAddingTimeInterval: 86400*6] completeBlock:^(NSInteger day, NSInteger hour, NSInteger minute, NSInteger second) {
        @strongify(self);
        self.timeStr = [NSString stringWithFormat:@"%ld天%ld时%ld分%ld秒",day,hour,minute,second];
        dispatch_main_async_safe(^{
            [self.dataSource replaceObjectAtIndex:6 withObject: self.timeStr];
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:6 inSection:0];
            [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        });
    }];

}


#pragma mark- 弹出计算器
- (void)alertCaltorAction:(UIButton *)button{
    
    
}

#pragma mark- 立即投标
- (void)immediatelyLoanAction{
    [self pushPageWithClass:[RNOLBuyClaimViewController class]];
}

#pragma mark -  UITableViewDelegate && UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataDict.allKeys.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *items = self.dataDict[@(section).stringValue];
    return items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *items = self.dataDict[@(indexPath.section).stringValue];
    RNOLInvestDetailListCell *cell = [tableView dequeueReusableCellWithIdentifier:RNOLInvestDetailListCell.cellReuseID];
    cell.valueLabel.text = indexPath.section? @"" : self.dataSource[indexPath.row];
    cell.nameLabel.text = items[indexPath.row];
    cell.bottomLineView.hidden = cell.rightArrowImageView.hidden = !indexPath.section;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [RNOLUnitTool createViewWithBackgroundColor:kBgColor];
}

- (void)viewDidLayoutSubviews
{
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *items = self.dataDict[@(indexPath.section).stringValue];
    NSString *title = items[indexPath.row];
    if (kCompareTwoString(title,@"项目信息")) {
        
    }else if (kCompareTwoString(title, @"投标记录")){
        [self pushPageWithClass:[RNOLBidRecordListViewController class]];
    }else if (kCompareTwoString(title, @"协       议")){
        
    }
    [self showStatusTipWithText: title];
}


@end
